package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IEmployeeService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("/login")
    @ResponseBody
    public JsonResult login(String name, String password, HttpSession session) {
        try {
            //用shiro的方法判断是否登录
            //封装令牌
            UsernamePasswordToken token = new UsernamePasswordToken(name, password);
            SecurityUtils.getSubject().login(token);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "账号或者密码错误");
        }
    }
    @RequestMapping("/logOut")
    public String logOut(HttpSession session){
        //销毁会话
        session.invalidate();
        //重定向
        return "redirect:/login.html";
    }
}
